package org.hw.juc.thread;

import java.util.concurrent.atomic.AtomicInteger;

public class ThreadLocalDemo {
    private static final ThreadLocal<Integer> local= ThreadLocal.withInitial(() -> 0);

    AtomicInteger counter;

    static void setLocal(Integer value) {
        local.set(value);
    }

    static Integer getLocal() {
        return local.get();
    }

    public static void main(String[] args) {

        new Thread(()->{
           setLocal(0);
            try {
                for (int i = 0; i <10; i++) {
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread()+" : "+getLocal());
                    setLocal(getLocal()+1);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
           setLocal(10);
            try {
                for (int i = 0; i <10; i++) {
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread()+" : "+getLocal());
                    setLocal(getLocal()+1);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }
}
